﻿using System;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
using System.Web.Script.Serialization;

namespace Ayatta.Common.Extensions
{
    public static class ObjectExtension
    {
        [DebuggerStepThrough]
        //public static bool IsIn(this object o, IEnumerable collection)
        //{
        //    return collection.Cast<object>().Contains(o);
        //}
        public static bool IsIn<T>(this T t, IEnumerable<T> collection)
        {
            return collection.Contains(t);
        }
        [DebuggerStepThrough]
        public static bool In<T>(this T t, params T[] c)
        {
            return c.Any(i => i.Equals(t));
        }

        [DebuggerStepThrough]
        public static string ToJson(this object o)
        {
            var scriptSerializer = new JavaScriptSerializer();
            return scriptSerializer.Serialize(o);
        }

        [DebuggerStepThrough]
        public static string ToJson(this object o, int recursionLimit)
        {
            var scriptSerializer = new JavaScriptSerializer {RecursionLimit = recursionLimit};
            return scriptSerializer.Serialize(o);
        }

        public static string ConvertToString(this byte[] source)
        {
            var hex = BitConverter.ToString(source);
            return hex.Replace("-", "");
        }

        public static byte[] ToByteArray(this string source)
        {
            var length = source.Length;
            var bytes = new byte[length/2];
            for (var i = 0; i < length; i += 2)
            {
                bytes[i/2] = Convert.ToByte(source.Substring(i, 2), 16);
            }
            return bytes;
        }

        //public static string TimestampToString(this byte[]  binary)
        //{
        //    byte[] binarybytes = binary.ToArray();
        //    string result = "";
        //    foreach (byte b in binarybytes)
        //    {
        //        result += b.ToString() + "|";
        //    }
        //    result = result.Substring(0, result.Length - 1);
        //    return result;
        //}

        //public static byte[] StringToTimestamp(this string s)
        //{
        //    string[] arr = s.Split('|');
        //    byte[] result = new byte[arr.Length];
        //    for (int i = 0; i < arr.Length; i++)
        //    {
        //        result[i] = Convert.ToByte(arr[i]);
        //    }
        //    return result;
        //}
        public static string TimestampToString(this byte[] binary)
        {
            return BitConverter.ToUInt64(binary.ToArray(), 0).ToString();
        }

        public static byte[] StringToTimestamp(this string s)
        {
            return BitConverter.GetBytes(Convert.ToUInt64(s));
        }

        public static ulong TimestampToUlong(this byte[] binary)
        {
            return BitConverter.ToUInt64(binary, 0);
        }

        public static byte[] UlongToTimestamp(this ulong value)
        {
            return BitConverter.GetBytes(value);
        }
    }
}
